package com.gable.exhibition.service.impl;

import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.UUIDGenerator;
import com.gable.exhibition.dao.FileDao;
import com.gable.exhibition.dao.NewsDao;
import com.gable.exhibition.model.File;
import com.gable.exhibition.model.News;
import com.gable.exhibition.service.NewsService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.FileView;
import com.gable.exhibition.view.NewsView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Lesson on 2018/3/9.
 */
@Service("newsService")
public class NewsServiceImpl implements NewsService {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    NewsDao newsDao;

    @Autowired
    FileDao fileDao;

    @Override
    public ReturnResultUtil queryNewsList(NewsView newsView) {
        int count = newsDao.queryNewsCount(newsView);
        List<NewsView> list = newsDao.queryNewsList(newsView);
        NewsView newsView1 = new NewsView();
        newsView1.setCount(count);
        newsView1.setList(list);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,newsView1);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil saveNews(NewsView newsView) {
        int result = 0;
        if(newsView.getNewsId() == null){
            newsView.setNewsId(UUIDGenerator.getUniqueLong());
            newsView.setIsDel(0);
            result = newsDao.insertSelective(newsView);
        }else{
            result = newsDao.updateByPrimaryKeySelective(newsView);
        }
        if(result > 0){
            if(newsView.getFileViewList() != null && newsView.getFileViewList().size() > 0){
                File file = new File();
                file.setKeyType(1406L);
                file.setKeyId(newsView.getNewsId());
                file.setIsDel(1);
                fileDao.deleteByKeyTypeKeyId(file);

                for(FileView f : newsView.getFileViewList()){
                    f.setKeyId(newsView.getNewsId());
                    fileDao.updateByPrimaryKeySelective(f);
                }
            }
            return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
        }
        return new ReturnResultUtil(500,MessageUtil.SENDFAIL);
    }

    @Override
    public ReturnResultUtil queryNewsInfo(Long newsId) {
        NewsView newsView = newsDao.queryNewsInfo(newsId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,newsView);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil withdraw(Long newsId) {
        News news = newsDao.selectByPrimaryKey(newsId);
        if(news == null){
            return new ReturnResultUtil(500,MessageUtil.WITHDRAWNEWSISNULL);
        }
        news.setStatus(2L);
        int result = newsDao.updateByPrimaryKey(news);
        if(result > 0){
            return new ReturnResultUtil(200,MessageUtil.WITHDRAWSUCCESS);
        }
        return new ReturnResultUtil(500,MessageUtil.WITHDRAWFAIL);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil publisNews(Long newsId) {
        News news = newsDao.selectByPrimaryKey(newsId);
        if(news == null){
            return new ReturnResultUtil(500,MessageUtil.PUBLISNEWSISNULL);
        }
        News news1 = new News();
        news1.setNewsId(newsId);
        news1.setStatus(1L);
        int result = newsDao.updateByPrimaryKeySelective(news1);
        if(result > 0){
            return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEFAIL);
    }
}
